package com.k4rthik.clipall.ui;

import com.k4rthik.clipall.commons.PropertyHandler;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created with IntelliJ IDEA.
 * User: Perseus
 * Date: 8/4/12
 * Time: 12:06 AM
 */

public class MainWindowPanel extends JPanel
{
    private MainWindowPanel()
    {
        createWindowTabs();
    }

    private void createWindowTabs()
    {
        JTabbedPane tabbedPane = new JTabbedPane();

        // Connections tab
        JPanel viewConnectionsPanel = createInnerPanel("Connections");
        tabbedPane.addTab(makeHTMLTitle("Connections"), null, viewConnectionsPanel, "Connections");

        // Connect to new system
        JPanel newConnectionsPanel = new NewConnectionsTab();
        tabbedPane.addTab(makeHTMLTitle("New Connection"), null, newConnectionsPanel, "New Connection");

        // Edit Profiles
        JPanel profilesPanel = createInnerPanel("Profiles");
        tabbedPane.addTab(makeHTMLTitle("Profiles"), null, profilesPanel, "Profiles");

        JPanel aboutMePanel = new AboutTab();
        tabbedPane.addTab(makeHTMLTitle("About"), null, aboutMePanel, "About");

        // Add the tabbed pane to this panel.
        tabbedPane.setSelectedIndex(0);
        setLayout(new GridLayout(1,1));
        add(tabbedPane);
    }

    private JPanel createInnerPanel(String panelSource)
    {
        JPanel tabPanel = new JPanel();
        JLabel tabSource = new JLabel(panelSource);

        tabSource.setHorizontalAlignment(JLabel.CENTER);
        tabPanel.setLayout(new GridLayout(1,1));
        tabPanel.add(tabSource);

        return tabPanel;
    }

    private String makeHTMLTitle(String plainTitle)
    {
        return ("<html><body><table height='150'>"+plainTitle+"</table></body></html>");
    }

    public static void build()
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            /* Ignore */
        }

        JFrame mainFrame = new JFrame("ClipB-Share :: A Clipboard Sharing App by Perseus");                     // Main Program Window

        mainFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(PropertyHandler.get("PROGRAM_ICON")));   // Main Program Icon
        mainFrame.addWindowListener(new WindowAdapter()                                                         // Exit event handler
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });

        //Main Window Properties
        mainFrame.getContentPane().add(new MainWindowPanel(), BorderLayout.CENTER);
        mainFrame.setBounds(10, 10, 450, 300);
        mainFrame.setSize(450, 300);
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
